package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

import vy.org.util.DateUtil;

public class VyEmployeeDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE EMPLOYEE_ID = m_Responsibilityid";
	private static final String FIND_BY_PARAMETER = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET VY_ID=m_VyId,FULL_NAME=m_FullName,WORK_SCOPE=m_WorkScope,WORK_LOCATION=m_WorkCitycode,SANT_SAMAJ_ID=m_SantSamajId,ASHRAM_ID=m_Ashramid,DESIGNATION_ID=m_Designationid,ROLE_RESPONSIBILITY=m_RolesResponsibility,START_DATE=m_StartDate,END_DATE=m_EndDate,LAST_UPDATED_BY=m_LastUpdatedBy,LAST_UPDATED_ON=m_LastUpdatedOn WHERE EMPLOYEE_ID = m_Responsibilityid";
	private static final String INSERT = "INSERT INTO <TABLE> (EMPLOYEE_ID,VY_ID,FULL_NAME,WORK_SCOPE,WORK_LOCATION,SANT_SAMAJ_ID,ASHRAM_ID,DESIGNATION_ID,ROLE_RESPONSIBILITY,START_DATE,END_DATE,LAST_UPDATED_BY,LAST_UPDATED_ON) VALUES(m_EmployeeId,m_VyId,m_FullName,m_WorkScope,m_WorkLocation,m_SantSamajId,m_AshramId,m_DesignationId,m_RolesResponsibility,m_StartDate,m_EndDate,m_LastUpdatedBy,m_LastUpdatedOn)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE VY_ID = m_VyId";

	public static VyEmployeeDO findByPK(String key) throws Exception {
		VyEmployeeDO dto = new VyEmployeeDO();
		dto.setEmployeeId(key);
		dto = (VyEmployeeDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static VyEmployeeDO insert(VyEmployeeDO dto) throws Exception {

		dto = (VyEmployeeDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyEmployeeDO delete(String key) throws Exception {
		VyEmployeeDO dto = new VyEmployeeDO();
		dto.setEmployeeId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

	public static VyEmployeeDO update(VyEmployeeDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyEmployeeDO insert(String key, String vyId, String fullName,
			String workScope, String workCitycode, String santSamajId,
			String ashramid, String designationid, String roles,
			String startDate, String endDate, String lastUpdatedBy,
			String lastUpdatedOn) throws Exception {

		VyEmployeeDO dto = new VyEmployeeDO();
		dto.setEmployeeId(key);
		dto.setVyId(vyId);
		dto.setFullName(fullName);
		dto.setWorkScope(workScope);
		dto.setWorkLocation(workCitycode);
		dto.setSantSamajId(santSamajId);
		dto.setAshramId(ashramid);
		dto.setDesignationId(designationid);
		dto.setRolesResponsibility(roles);
		dto.setStartDate(startDate);
		dto.setEndDate(endDate);
		dto.setLastUpdatedBy(lastUpdatedBy);
//		dto.setLastUpdatedOn(DateUtil.getCurrentDate().toString());

		dto = (VyEmployeeDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	@SuppressWarnings("unchecked")
	public static List<VyEmployeeDO> findByParam(VyEmployeeDO dto)
			throws Exception {

		StringBuilder sb = new StringBuilder(FIND_BY_PARAMETER);
		if (dto.getEmployeeId() != null) {
			sb.append(" AND EMPLOYEE_ID LIKE '%" + dto.getEmployeeId()
					+ "%' ");
		}
		if (dto.getVyId() != null) {
			sb.append(" AND VY_ID LIKE '%" + dto.getVyId() + "%' ");
		}
		
		if (dto.getFullName() != null) {
			sb.append(" AND FULL_NAME LIKE '%" + dto.getFullName() + "%' ");

		}
		if (dto.getAshramId() != null) {
			sb.append(" AND ASHRAM_ID LIKE '%" + dto.getAshramId() + "%' ");
		}
		if (dto.getDesignationId() != null) {
			sb.append(" AND DESIGNATION_ID LIKE '%" + dto.getDesignationId()
					+ "%' ");
		}
		if (dto.getRolesResponsibility() != null) {
			sb.append(" AND ROLE_RESPONSIBILITY LIKE '%"
					+ dto.getRolesResponsibility() + "%' ");

		}
		if (dto.getWorkLocation() != null) {
			sb.append(" AND WORK_LOCATION LIKE '%" + dto.getWorkLocation()
					+ "%' ");

		}
		if (dto.getSantSamajId() != null) {
			sb.append(" AND SANT_SAMAJ_ID LIKE '%" + dto.getSantSamajId()
					+ "%' ");
		}

		if (dto.getStartDate() != null) {
			sb.append(" AND START_DATE LIKE '%" + dto.getStartDate() + "%' ");

		}
		if (dto.getEndDate() != null) {
			sb.append(" AND END_DATE LIKE '%" + dto.getEndDate() + "%' ");

		}
		if (dto.getLastUpdatedBy() != null) {
			sb.append(" AND LAST_UPDATED_BY LIKE '%" + dto.getLastUpdatedBy()
					+ "%' ");

		}
		if (dto.getLastUpdatedOn() != null) {
			sb.append(" AND LAST_UPDATED_ON LIKE '%" + dto.getLastUpdatedOn()
					+ "%' ");

		}
		if (dto.getWorkScope() != null) {
			sb.append(" AND WORK_SCOPE LIKE '%" + dto.getWorkScope() + "%' ");

		}
		System.out.println(sb.toString());
		return (List<VyEmployeeDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}
}
